package com.zxc.util;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

import javax.sql.DataSource;

import org.apache.commons.dbcp.BasicDataSourceFactory;
import org.apache.log4j.Logger;


public class MySqlDBUtil {
	private static Properties properties;
	
	private static DataSource ds ;
	
	private static final Logger log = Logger.getLogger(MySqlDBUtil.class);
	static{
		properties = new Properties();
		FileInputStream fis = null;
		try {
			fis = new FileInputStream("src/MySql.properties");
			properties.load(fis);
			ds = BasicDataSourceFactory.createDataSource(properties);
		} catch (FileNotFoundException e) {
			log.info(e);
		} catch (IOException e) {
			log.info(e);
		} catch (Exception e) {
			log.info(e);
		} finally{
			if(fis != null){
				try {
					fis.close();
				} catch (IOException e) {
					log.info(e);
				}
			}
			
			try {
				getConnection().close();
			} catch (SQLException e) {
				log.info(e);
			}
		}
	}
	
	public static Connection getConnection() throws SQLException{
		return ds.getConnection();
	}
	
}
